#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

int main()
{
  int pipefd[2]={0};
  if(pipe(pipefd)!=0)
  {
    printf("pipe fail\n");
    return 1;
  }

  printf("pipefd[0]: %d\n",pipefd[0]);
  printf("pipefd[1]: %d\n",pipefd[1]);

  if(fork()==0)
  {
    //child
    close(pipefd[0]);

    
    const char* msg="hello world";
    while(1){
      write(pipefd[1],msg,strlen(msg));
      sleep(1);
    }
    exit(0);
  }
// parent;
  close(pipefd[1]);
  while(1)
  {
    char buffer[64];
    ssize_t s=read(pipefd[0],buffer,sizeof(buffer));

    if(s==0)
    {
      break;
    }
    else if(s>0)
    {
      buffer[s]=0;
      printf("child say to father# %s\n",buffer);
    }
    else
    {
      break;
    }
  }
  return 0;
}
